package com.wjggy.core.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

/**
 * 日期时间工具类
 * @author 余小凡 
 * 2010-1-18
 */
public class DateUtil {
    private static final String FORMAT_yyyy_MM_dd = "yyyy-MM-dd";

    private static final String FORMAT_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    public static final String formatDatetime(String format, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static final String formatDatetime(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }

    public static final String dateTime2Str(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static final Date str2Date(String dateStr) throws Exception {
        return str2Date(dateStr, "-");
    }

    public static final Date str2Date(String dateStr, String delim) throws Exception {
        if (StringUtils.isEmpty(dateStr) == true) {
            return new Date();
        }

        StringTokenizer stringTokenizer = new StringTokenizer(dateStr, delim);
        if (stringTokenizer.countTokens() != 3) {
            throw new Exception("date format error!");
        }

        int year = Integer.parseInt(stringTokenizer.nextToken());
        int month = Integer.parseInt(stringTokenizer.nextToken());
        int day = Integer.parseInt(stringTokenizer.nextToken());

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        return calendar.getTime();
    }
    /**
     * 
     * @param date
     * @return yyyy-MM-dd
     */
    public static final String date2Str(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }
    /**
     * yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static final String date2Str2(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }
    
    /**
     * yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static final String date2Str2() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }
    public static final Date dateStr2DateTime(String dateStr, String ext) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_MM_dd_HH_mm_ss);
        dateFormat.setLenient(false);
        Date date = dateFormat.parse(dateStr + " " + ext, new ParsePosition(0));
        return date;
    }
    /**
     * 方法名称：getDatae2Str
     * 创建时间：2011-6-14:下午10:11:33
     * 方法描述：返回精确到时分秒的时间
     * 执行流程：
     * @return
     */
    public static final String getDatae2Str(){
    	 Calendar calendar = Calendar.getInstance();
    	return  date2Str2(calendar.getTime());
    }
    public static final Date dateStr2DateTime(String dateStr){
    	SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_MM_dd_HH_mm_ss);
    	dateFormat.setLenient(false);
    	Date date = dateFormat.parse(dateStr, new ParsePosition(0));
    	return date;
    }
    
    public static final long getStartDateTimeInMillis(String date) {
        return getTimeInMillis(dateStr2DateTime(date, "00:00:00"));
    }
    
    public static final long getEndDateTimeInMillis(String date) {
        return getTimeInMillis(dateStr2DateTime(date, "23:59:59"));
    } 
    
    public static final long getTimeInMillis(Date dateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        return calendar.getTimeInMillis();
    }
    /**
     * 方法描述：获取当前时间
     * 执行流程：获取long类型当前时间
     * @return
     */
    public static final long getTimeLong() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTimeInMillis();
    }
    public static String calendar2StrDateTime(Calendar calendar) {
        return date2StrDateTime(calendar.getTime());
    }
    
    public static String date2StrDateTime(Date date) {
        return DateFormatUtils.format(date, FORMAT_MM_dd_HH_mm_ss);
    }

    public static final Date now() {
        return new Date();
    }

    public static final String nowStr() {
        return dateTime2Str(new Date());
    }

    public static final String dateStr() {
        return date2Str(new Date());
    }

    public static final String getTime() {
        return String.valueOf(new Date().getTime());
    }

    public static final String getSecond() {
        return getTime().substring(0, 10);
    }

    public static final String getStrCurrYear() {
        return formatDatetime("yyyy", new Date());
    }

    public static final String getStrCurrMonth() {
        return formatDatetime("MM", new Date());
    }

    public static final String getStrCurrDay() {
        return formatDatetime("dd", new Date());
    }

    public static final String getStrCurrHour() {
        return formatDatetime("HH", new Date());
    }

    public static final String getStrCurrMinute() {
        return formatDatetime("mm", new Date());
    }

    public static final int getIntCurrYear() {
        return Integer.parseInt(getStrCurrYear());
    }

    public static final int getIntCurrMonth() {
        return Integer.parseInt(getStrCurrMonth());
    }

    public static final int getIntCurrDay() {
        return Integer.parseInt(getStrCurrDay());
    }

    public static final int getIntCurrHour() {
        return Integer.parseInt(getStrCurrHour());
    }

    public static final int getIntCurrMinute() {
        return Integer.parseInt(getStrCurrMinute());
    }

    public int getSeason() {
        int i = getIntCurrMonth();
        if ((1 <= i) && (i <= 3)) {
            return 1;
        }
        if ((4 <= i) && (i <= 6)) {
            return 2;
        }
        if ((7 <= i) && (i <= 9)) {
            return 3;
        }
        return 4;
    }
    public static final String today() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        return simpleDateFormat.format(new Date());
    }
    public static String fmtDateCNStr(String date){
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	DateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日");
    	Date d=null;
    	try {    
    		  d = format.parse(date);  // Wed sep 26 00:00:00 CST 2007    
    	} catch (Exception e) {    
    		 e.printStackTrace();    
    	}  
    	return format2.format(d);  
    }
    
    public static String fmtDateStr(String date){
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date d=null;
    	try {    
    		  d = format.parse(date);  // Wed sep 26 00:00:00 CST 2007    
    	} catch (Exception e) {    
    		 e.printStackTrace();    
    	}  
    	return format2.format(d);  
    }
    
    /**
     * 
     * @return yyyyMMddHHmmss
     */
    public static String timeLine(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
       return simpleDateFormat.format(new Date());
    }
    /**
     * 
     * @return yyyyMMdd
     */
    public static String ymd(String date){
    	Date d=dateStr2DateTime(date);
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    	return simpleDateFormat.format(d);
    }
    /**
     * 
     * @return yyyyMMdd
     */
    public static String ymd(){
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    	return simpleDateFormat.format(new Date());
    }
    /**
     * 计算剩余时间,返回形式 xx年XX月xx日XX小时xx分xx秒
     * @param beginTimes
     * @param endTimes
     * @return
     */
    public static String remainTimes(String beginTimes,String endTimes){
    	SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	try {
        java.util.Date begin;
        java.util.Date end;
		
			begin = dfs.parse(beginTimes);
			end = dfs.parse(endTimes);
			return remainTimes(begin.getTime(),end.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    /**
     * 计算剩余时间,返回形式 xx年XX月xx日XX小时xx分xx秒
     * @param beginTimes
     * @param endTimes
     * @return
     */
    public static String remainTimes(long beginTimes,long endTimes){
    	long between=(endTimes-beginTimes)/1000;//除以1000是为了转换成秒
    	
    	 StringBuffer sb=new StringBuffer();
    	 
    	if(endTimes>beginTimes){
	        long day=between/(24*3600);
	        long hour=between%(24*3600)/3600;
	        long minute=between%3600/60;
	        long second=between%60/60;
	    //    System.out.println(""+day+"天"+hour+"小时"+minute+"分"+second+"秒");
	        sb.append("离活动结束剩余：");
	        if(day>0){
	        	sb.append(day+"天");
	        }
	        if(hour>0){
	        	sb.append(hour+"小时");
	        }
	        if(minute>0){
	        	sb.append(minute+"分");
	        }
	       // if(second>0){
	        	sb.append(second+"秒");
	      //  }
    	}else{
    		sb.append("");
    	}
    	return sb.toString();
    }
     /**
      * 日期相减-今日起时间往后减
      * @param subDay
      * @return yyyy-mm-dd
      */
      public static String todaySub(int subDay){   
          GregorianCalendar calendar = new GregorianCalendar();   
 
          int day = calendar.get(Calendar.DAY_OF_YEAR);
          calendar.set(Calendar.DAY_OF_YEAR, day - subDay);
          Date yearDay = calendar.getTime();   
          DateFormat df = DateFormat.getDateInstance();   
          String preYearDay = df.format(yearDay);   
  
          return preYearDay;   
     }
      /**
       * 日期相减-今日起时间往后减后的千分秒时间
       * @param subDay
       * @return calendar.getTimeInMillis()
       */
      public static long getTodaySubLong(int subDay){   
    	  GregorianCalendar calendar = new GregorianCalendar();   
    	  
    	  int day = calendar.get(Calendar.DAY_OF_YEAR);
    	  calendar.set(Calendar.DAY_OF_YEAR, day - subDay);
//    	  Date yearDay = calendar.getTime();   
//    	  DateFormat df = DateFormat.getDateInstance();   
//    	  String preYearDay = df.format(yearDay);   
    	  
    	  return calendar.getTimeInMillis();   
      }
      
      /**
       * 日期相加-今日起时间往后加小时
       * @param addDay
       * @return yyyy-mm-dd hh:mm:ss
       */
      public static Date dateAddHour(String dateHMS,int addDay){   
    	  GregorianCalendar calendar = new GregorianCalendar();   
    	  Date date=dateStr2DateTime(dateHMS);
    	  calendar.setTime(date);
    	  int day = calendar.get(Calendar.HOUR_OF_DAY);
    	  calendar.set(Calendar.HOUR_OF_DAY, day + addDay);
    	  Date addDate=calendar.getTime();
    	  return addDate;
     }
      /**
       * 给定时间是否在当前时间之后 给定时间：日期相加-dateHMS加addDay小时输后相比
       * @param addDay
       * @return calendar.getTimeInMillis()
       */
     public static  boolean afterDateAddHour(String dateHMS,int addDay){
    	 Date date=dateAddHour(dateHMS,addDay);
    	return new Date().getTime()>date.getTime();
     }
      public static void main(String[] args) {
		System.out.println(DateUtil.date2Str2(DateUtil.dateAddHour(DateUtil.date2Str2(), 3)));
	}
      /**
       * 日期相加-今日起时间往后加
       * @param addDay
       * @return yyyy-mm-dd
       */
      public static String todayAdd(int addDay){   
    	  GregorianCalendar calendar = new GregorianCalendar();   
    	  int day = calendar.get(Calendar.DAY_OF_YEAR);
    	  calendar.set(Calendar.DAY_OF_YEAR, day + addDay);
    	  Date yearDay = calendar.getTime();   
    	  DateFormat df = DateFormat.getDateInstance();   
    	  String preYearDay = df.format(yearDay);    
    	  return preYearDay; 
     }
      /**
       * 日期相加-今日起时间往后加后的千分秒时间  分钟
       * @param addDay
       * @return calendar.getTimeInMillis()
       */
      public static long getTodayAddMinuteLong(int addDay){   
    	  GregorianCalendar calendar = new GregorianCalendar();   
    	  
    	  int day = calendar.get(Calendar.MINUTE);
    	  calendar.set(Calendar.MINUTE, day + addDay);
    	  return calendar.getTimeInMillis();   
      }
      
      /**
       * 日期相加-今日起时间往后加后的千分秒时间
       * @param addDay
       * @return calendar.getTimeInMillis()
       */
      public static long getTodayAddLong(int addDay){   
    	  GregorianCalendar calendar = new GregorianCalendar();   
    	  
    	  int day = calendar.get(Calendar.DAY_OF_YEAR);
    	  calendar.set(Calendar.DAY_OF_YEAR, day + addDay);
    	  return calendar.getTimeInMillis();   
      }
    /**
     * 判定有效结束日期date是否在本日之后  
     * @param date yyyy-MM-dd
     * @return
     */
    public static boolean afterToday(String date){
    	boolean after=false;
    	long end=getEndDateTimeInMillis(date);
    	long now=getTimeInMillis(new Date());
    	if(end>now){
    		after=true;
    	}
    	return after;
    }  
    /**
     * 判定有效结束日期date是否在本日之后  
     * @param date yyyy-MM-dd
     * @return
     */
    public static boolean afterToday(long start){
    	boolean after=false;
    	long now=getTimeInMillis(new Date());
    	if(start>now){
    		after=true;
    	}
    	return after;
    }  
    /**
     * 日期相加-今日起时间往后加
     * @param addDay
     * @return yyyy-mm-dd
     */
    public static Calendar getCalendar_TodayAdd(int addDay){   
   	 GregorianCalendar calendar = new GregorianCalendar();   
   	 
   	 int day = calendar.get(Calendar.DAY_OF_YEAR);
   	 calendar.set(Calendar.DAY_OF_YEAR, day + addDay);
//   	 Date yearDay = calendar.getTime();   
//   	 DateFormat df = DateFormat.getDateInstance();   
//   	 String preYearDay = df.format(yearDay);   
//   	 
//   	 return preYearDay; 
   	 return calendar;
   	 
    }
    /**
     * Calendar转换到字符串yyyy-MM-dd
     * @param calendar Calendar
     * @return String yyyy-MM-dd
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String calendar2StrDate(Calendar calendar) {
        return date2Str(calendar.getTime());
    }
  
}
